// src/store/modules/qrcode.store.ts
import QRCode from 'qrcode'

export default {
    state: () => ({
        qrcode: '' as string,
        expired: false
    }),

    actions: {
        async generate(this: any) {
    try {
        this.qrcode = await QRCode.toDataURL('weixin://dl/chat', {
            width: 240,
            color: { dark: '#333' }
        })
        this.startTimer()
    } catch (error) {
        console.error('二维码生成失败:', error)
    }
},

startTimer(this: any) {
    this.expired = false
    const timer = setTimeout(() => {
        this.expired = true
    }, 3000)

    // 可选：添加定时器清理逻辑
    this.$onAction(({ name, after }) => {
        if (name === 'generate') {
            after(() => clearTimeout(timer))
        }
    })
}
}
}